package wyh.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MyBatisUtils {
    private static SqlSessionFactory sessionFactory=null;
    static {
        try {
            Reader reader= Resources.getResourceAsReader("mybatais-config.xml");
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            sessionFactory= builder.build(reader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
                        
    public static SqlSession getSession(Boolean b){
        return sessionFactory.openSession(b);
    }
}
/*public class MyBatisUtils {
    //    定义一个sql session Factory工厂类对象
    private static SqlSessionFactory sqlSession = null;

    static {
        try {
//            reader对象被Resources资源根目录对象.get数据As Reader读mybatis-config.xml
            //注意是Resources不是Resource  有SSSSSSSSSSS！~！！！
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//          申明一个sqlssss工厂的构建对象
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//            工厂的构建对象根据reader对象 构建赋值 sqlSession对象
            sqlSession = builder.build(reader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static SqlSession getSqlSession(Boolean b) {
//      false则需手动调用sqlSession.commit()来提交事务，否则数据库中的数据不会更新
//        turn自动更新
        return sqlSession.openSession(b);
    }
}*/
